﻿/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/

#include "os_obj.h"
#define _SYSTASK_MODULE_
#include "sys_task.h"
#undef _SYSTASK_MODULE_
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"

void sys_led_ctr(void);
void system_info(char pos);
void sys_ui_tst(void);
void at_flash_test(void);

int core_main(int argc, char *argv[]);
void KeyTask(void const * argument);

uint8_t *mem_tst[4];

char * listn[] =
{
  [0] = { "new""new2" }, //编译器链接为一个字符串
  [1] = { "en" },
};

/*****************************************************************************
*
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(void * thread_input)
{
  (void)thread_input;

  uint32_t us_tick_s, us_tick_end;

  sys_init();

  system_info(0);

  mem_tst[0] = mem_malloc(100);
  mem_tst[1] = mem_malloc(200);
  mem_tst[2] = mem_malloc(300);
  mem_tst[3] = mem_malloc(400);
  
  mem_free(mem_tst[2]);
  mem_tst[2] = mem_malloc(300);
  
  mem_free(mem_tst[1]);
  mem_tst[1] = mem_malloc(1020);
  
  mem_free(mem_tst[0]);
  mem_tst[0] = mem_malloc(150*1024);
  mem_free(mem_tst[0]);
  
  mem_ccm.api->calloc(1, 128);
  
  //at_flash_test();

  while (1)
  {
    bsp_dwt_check();

    KeyTask(0);
    
    us_tick_s = app_tick_us_get();

    sys_st.u_tick = app_tick_get();

    if (sys_st.b_coremark)
    {
      sys_st.b_coremark = 0;
    }

    pulse_create(&sys_st.pulse, sys_st.u_tick);
    if (sys_st.pulse.bPulse_1s)
    {
      TimeRun(&sys_st.rtc_time);
      LED_CPU = !LED_CPU;
    }

    sys_st.sig_time = time_to_sig(&sys_st.rtc_time);
    time_sig_to_time(sys_st.sig_time, &sys_st.rtc_time);


    sys_led_ctr();

    us_tick_end = app_tick_us_get();

    sys_st.exe_time_us = us_tick_end - us_tick_s;

    if (sys_st.exe_time_us_max < sys_st.exe_time_us)
    {
      sys_st.exe_time_us_max = sys_st.exe_time_us;
    }

    NOP1();
    osDelay(ms_ticks(10));
    NOP1();
  }
}

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
  if (sys_st.pulse.bPulse_1s)
  {
    //O_LED1.dir_out_pp();
    //O_LED1.tog();
  }

  sys_st.led_obj.IncCoef = 50;
  sys_st.led_obj.min = 1;
  sys_st.led_obj.max = 900;
  sys_st.led_obj.max_keep_time_sv = 2;
  sys_st.led_obj.min_keep_time_sv = 2;
  led_breath_handle(&sys_st.led_obj);

}

/*****************************************************************************
*
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_sw_hook(char pos)
{

}

/*****************************************************************************
*
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(char pos)
{
  sys_st.clk.SYS = HAL_RCC_GetSysClockFreq();
  sys_st.clk.AHB = HAL_RCC_GetHCLKFreq();
  sys_st.clk.APB1 = HAL_RCC_GetPCLK1Freq();
  sys_st.clk.APB2 = HAL_RCC_GetPCLK2Freq();
  
  dbg_puts("\n");
  dbg_puts("==========================================\n");
  dbg_print("compile time: %s  %s\n", __DATE__, __TIME__);
  dbg_puts("RTOS : ThreadX\n");
  dbg_print("AHB CLK: %d MHz\n", sys_st.clk.AHB/1000000);
  dbg_print("APB1 CLK: %d MHz\n", sys_st.clk.APB1/1000000);
  dbg_print("APB2 CLK: %d MHz\n", sys_st.clk.APB2/1000000);
  dbg_puts("==========================================\n");

}

/*****************************************************************************
* @brief   task switch hook.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_pre_init(char pos)
{

  sys_st.b_coremark = 0;
}
OBJ_INIT_APP_EXPORT(sys_pre_init);


/*******************************************************************************
* @brief  at_flash_test
* \param[in] none
* \retval: none
*******************************************************************************/
void at_flash_test(void)
{

}

uint32_t rt_tick_get(void)
{
  return sys_st.u_tick;
}

/*******************************************************************************
* @brief  lib surport
* \param[in] none
* \retval: none
*******************************************************************************/
#define LRC_ADDR01		0x20000000
#define LRC_ADDR02		0x20000004
#define LRC_ADDR03		0x20000008
#define LRC_ADDR04		0x2000000C
#define LRC_ADDR0_RES	0x20000010
#define LRC_ADDR0_NRES	0x20000014

void lib_os_lock(void)
{
	
}

void lib_os_unlock(void)
{

}

void lib_check_hook(int32_t st)
{
    uint32_t lrc;

	if(st > 2)
	{
		lrc = 0;
		lrc += inpw(LRC_ADDR01);
		lrc += inpw(LRC_ADDR02);
		lrc += inpw(LRC_ADDR03);
		lrc += inpw(LRC_ADDR04);
		
		outpw(LRC_ADDR0_RES, lrc );
		outpw(LRC_ADDR0_NRES, ~lrc);
	}
}
